//
//  String.swift
//  Networking
//
//  Created by 许必成 on 2020/7/22.
//  Copyright © 2020年 东信北邮信息技术有限公司. All rights reserved.
//

import UIKit

extension String {
    /// 截取字符串
    ///
    /// - Parameter index: 截取从index位开始之前的字符串
    /// - Returns: 返回一个新的字符串
    func mySubString(to index: Int) -> String {
        return String(self[..<self.index(self.startIndex, offsetBy: index)])
    }
    /// 截取字符串
    ///
    /// - Parameter index: 截取从index位开始到末尾的字符串
    /// - Returns: 返回一个新的字符串
    func mySubString(from index: Int) -> String {
        return String(self[self.index(self.startIndex, offsetBy: index)...])
    }
    
    /// 根据字符串，返回通知名字
    var notifiName: NSNotification.Name {
        return NSNotification.Name(rawValue: self)
    }
    
    ///  根据名称，返回nib
    var nib:UINib  {
        return UINib(nibName: self, bundle: nil)
    }
    
    /// String 转化成 CGFloat
    var StringToFloat: CGFloat {
        var cgFloat:CGFloat = 0
        if let doubleValue = Double(self){ cgFloat = CGFloat(doubleValue) }
        return cgFloat
    }
    
    /// String 转化成 CGFloat
    var StringToDouble: Double {
        var d:Double = 0
        if let doubleValue = Double(self){ d = Double(doubleValue) }
        return d
    }
    
    /// String 转化成 Int
    var stringToInt: Int {
        var int:Int?
        if let doubleValue = Int(self) { int = Int(doubleValue) }
        if int == nil { return 0 }
        return int!
    }
    
    var isPhoneNumber: Bool {
        
        // \d => [0-9]
        let regex = "^1[3-9]\\d{9}"
//        let regex = "^1[3|4|5|6|7|8|9][0-9]\\d{8}$"
        
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return predicate.evaluate(with: self)
    }
}
extension String {
    
    var containsEmoji: Bool {
        for scalar in unicodeScalars {
            switch scalar.value {
            case
            0x00A0...0x00AF,
            0x2030...0x204F,
            0x2120...0x213F,
            0x2190...0x21AF,
            0x2310...0x329F,
            0x1F000...0x1F9CF:
                return true
            default:
                continue
            }
        }
        return false
    }
    
}

